//
//  UIViewController+Empty.m
//  WeiYiMei
//
//  Created by iMac on 2019/4/25.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "UIViewController+Empty.h"
#import <objc/runtime.h>

@interface UIViewController ()

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIImageView *noDataIcon;
@property (nonatomic, strong) UILabel *noDataLabel;

@end

@implementation UIViewController (Empty)

- (void)showEmptyView
{
    [self.contentView removeAllSubviews];
    
    self.contentView = [[UIView alloc] init];
    self.contentView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.contentView];
    
    self.noDataIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nodata"]];
    [self.contentView addSubview:self.noDataIcon];
    
    self.noDataLabel = [[UILabel alloc] init];
    self.noDataLabel.font = [UIFont systemFontOfSize:15];
    self.noDataLabel.textAlignment = NSTextAlignmentCenter;
    self.noDataLabel.text  = @"点击重试";
    self.noDataLabel.textColor = kColor999999;
    [self.contentView addSubview:self.noDataLabel];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
    }];
    
    [self.noDataIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.contentView).offset(20);
        make.right.equalTo(self.contentView).offset(-20);
    }];
    
    [self.noDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.noDataIcon.mas_bottom).offset(20);
        make.centerX.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView).offset(-20);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refreshLoad)];
    [self.contentView addGestureRecognizer:tap];
    self.contentView.userInteractionEnabled = YES;
}

- (void)refreshLoad
{
    
}

- (void)removeEmptyView
{
    [self.contentView removeFromSuperview];
}

- (void)setNoDataIcon:(UIImageView *)noDataIcon
{
    objc_setAssociatedObject(self, @selector(noDataIcon), noDataIcon, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIImageView *)noDataIcon
{
    return objc_getAssociatedObject(self, @selector(noDataIcon));
}

- (void)setContentView:(UIView *)contentView
{
    objc_setAssociatedObject(self, @selector(contentView), contentView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)contentView
{
    return objc_getAssociatedObject(self, @selector(contentView));
}

- (void)setNoDataLabel:(UILabel *)noDataLabel
{
    objc_setAssociatedObject(self, @selector(noDataLabel), noDataLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UILabel *)noDataLabel
{
    return objc_getAssociatedObject(self, @selector(noDataLabel));
}

@end
